package com.ddf.norman.util;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @ClassName: PasswordEncoder.java
 * @Description: 密码加盐工具类
 * @author: norman
 * @date:  2020-06-10 14:55
 * @version V1.0
 */
@Component
public class HashPasswordEncoder implements PasswordEncoder {
    //jerryjoseluckynormanplutozero SHA512
    private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

    private Object salt = "f60057e6b7454a00b3e8aa9f5b2dab951cc26afd1bcc8f354050b94e68c5c656828381b835313897152c5a407568339ed606d983855bd7bf35cbb3f1c670f6d5";

    private String algorithm = "MD5";

    @Override
    public String encode(CharSequence charSequence) {
        String result = null;
        try {
            MessageDigest md = MessageDigest.getInstance(algorithm);
            //加密后的字符串
            result = byteArrayToHexString(md.digest(mergePasswordAndSalt(charSequence.toString()).getBytes("utf-8")));
        } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public boolean matches(CharSequence charSequence, String encodedPassword) {
        if (charSequence == null) {
            throw new BadCredentialsException("密码错误");
        } else if (encodedPassword != null || encodedPassword.length() !=0) {
            MessageDigest md = null;
            String result = null;
            try {
                md = MessageDigest.getInstance(algorithm);
                result = byteArrayToHexString(md.digest(mergePasswordAndSalt(charSequence.toString()).getBytes("utf-8")));
            } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return encodedPassword.equals(result);
        } else {
            throw new BadCredentialsException("密码错误");
        }
    }

    private String mergePasswordAndSalt(String password) {
        if (password == null) {
            password = "";
        }

        if ((salt == null) || "".equals(salt)) {
            return password;
        } else {
            return password + "{" + salt.toString() + "}";
        }
    }

    /**
     * 转换字节数组为16进制字串
     * @param b 字节数组
     * @return 16进制字串
     */
    private static String byteArrayToHexString(byte[] b) {
        StringBuffer resultSb = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            resultSb.append(byteToHexString(b[i]));
        }
        return resultSb.toString();
    }

    private static String byteToHexString(byte b) {
        int n = b;
        if (n < 0)
            n = 256 + n;
        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }

}
